﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;

namespace Mang24k99.Web.OrgList
{
    public partial class BankAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bindate();
            }
        }
        public void Bindate()
        {
            string where = "orglist.ParentNO=3 order by orglist.LevelOrder";
            MKtCommOrglist[] orglist = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
            this.grdList.DataSource = orglist;
            this.grdList.DataBind();
        }
        /// <summary>
        /// 选择gridview中某一行进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = this.grdList.SelectedIndex;
            string orgno = this.grdList.Rows[index].Cells[1].Text;
            MKtCommOrglist orglist = BKtCommOrglist.Find(Int32.Parse(orgno));
            this.txtBankName.Text = orglist.OrgnizeName;
            this.txtOrgNO.Text = orglist.OrgnizeNO.ToString();
			rbtnListGrant.SelectedValue = orglist.IsGrant.ToString();
            Session["orgname"] = orglist.OrgnizeName;
            this.btnSave.Text = "更新";
        }

        /// <summary>
        /// 删除操作(逻辑删除)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MKtCommOrglist orglist = new MKtCommOrglist();
            int id = Int32.Parse(this.grdList.Rows[e.RowIndex].Cells[1].Text);
            orglist = BKtCommOrglist.Find(id);
            bool result = BKtCommOrglist.Delete(orglist);
            if (result == true)
            {
                string strMsg = "删除成功";
                Common.MessageBox.Show(this.Page, strMsg);
                Bindate();
                this.btnSave.Text = "新增";
                this.txtBankName.Text = "";
                this.txtOrgNO.Text = "";
            }
            else
            {
                string strMsg = "删除失败";
                Common.MessageBox.Show(this.Page, strMsg);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
          
            if (btnSave.Text=="新增")
            {
                if (PageValidate() == false)
                {
                    return;
                }
                MKtCommOrglist orglist = new MKtCommOrglist();
                MKtCommOrglist parent = BKtCommOrglist.Find(3);
                orglist.OrgnizeNO = (int)BKtCommOrglist.GetMaxNum() + 1;
                orglist.OrgnizeName = this.txtBankName.Text.Trim();
                orglist.ParentNO = 3;
                orglist.CompsiteNO = parent.CompsiteNO + "," + orglist.OrgnizeNO;
                orglist.IsValidate = 1;
                orglist.IsColumn = 1;
                orglist.IsGrant = Convert.ToInt32(rbtnListGrant.SelectedValue);
                orglist.OrangeLayer = 2;
                string where = "orglist.ParentNO=3 ";
                int order = BKtCommOrglist.SelectMaxOrder(where);
                orglist.LevelOrder = order + 1;
                orglist.InputTime = DateTime.Now;

                try
                {
                    BKtCommOrglist.Create(orglist);
                    Bindate();
                    this.txtBankName.Text = "";
                    string strMsg = "新增机构成功";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
            if (btnSave.Text=="更新")
            {
                if (this.txtBankName.Text.Trim() != Session["orgname"].ToString())
                {
                    if (PageValidate() == false)
                    {
                        return;
                    }
                }
                int orgno = Int32.Parse(this.txtOrgNO.Text);
                MKtCommOrglist orglist = (MKtCommOrglist)BKtCommOrglist.Find(orgno);
                orglist.OrgnizeName = this.txtBankName.Text.Trim();
				orglist.IsGrant = Convert.ToInt32(rbtnListGrant.SelectedValue);
                orglist.ModifyTime = DateTime.Now;
                try
                {
                    BKtCommOrglist.Update(orglist);
                    Bindate();
                    Session["orgname"] = null;
                    this.btnSave.Text = "新增";
                    this.txtBankName.Text = "";
                    this.txtOrgNO.Text = "";
                    string strMsg = "更新成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
                catch (System.Exception ex)
                {
                    throw;
                }
            }
           
        }
        /// <summary>
        /// 页面有效性验证
        /// </summary>
        /// <returns></returns>
        public bool PageValidate()
        {
            if (this.txtBankName.Text.Trim()== "")
            {
                string strMsg = "银行机构名称不能为空";
                Common.MessageBox.Show(this.Page, strMsg); 
                return false;
            }
            else
            {
                string where = "orglist.ParentNO=3 and orglist.OrgnizeName='"+this.txtBankName.Text.Trim()+"'";
                MKtCommOrglist[] list = (MKtCommOrglist[])BKtCommOrglist.SelectByWhere(where);
                if (list.Length != 0)
                {
                    string strMsg = "银行机构名称不能重复！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    return false;
                }
            }

            return true;
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnSave.Text = "新增";
            this.txtBankName.Text = "";
            this.txtOrgNO.Text = "";
        }
        public string GetStatus(int status)
        {
            if (status == 1)
            {
                return "显示";
            }
            else
            {
                return "隐藏";
            }
        }

        /// <summary>
        /// 隐藏机构
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "hidden")
            {
                MKtCommOrglist orglist = new MKtCommOrglist();
                int NO = Int32.Parse(e.CommandArgument.ToString());
                orglist = BKtCommOrglist.Find(NO);
                if (orglist.IsColumn == 1)
                {
                    orglist.IsColumn = 0;
                }
                else
                {
                    orglist.IsColumn = 1;
                }
                BKtCommOrglist.Update(orglist);
                Bindate();
            }
        }
    }
}
